import {Component, OnInit, AfterViewInit, ViewChild} from '@angular/core';
import {OneOperationComponent} from "../../../../widgets/one-operation/one-operation.component";
import {Router, ActivatedRoute} from "@angular/router";
import {TableUtil} from "../../../../util/table-util";
import {OneDialogService} from "../../../../widgets/one-dialog/one-dialog.service";
import {ResponseUtil} from "../../../../util/response-util";
import {PageSubject} from "../../../../util/page-subject";
import {Path} from "../../../../portal/admin/admin-breadcrumb/breadcrumb";
import {DamAPI} from "../../peach-constants";
import {MsgResult} from "../../../../models/msg-result";
import {MsgConstants} from "../../../../constants/constants";
import {EncircledAreaService} from "./encircled-area.service";

declare var $;

@Component({
  selector: 'app-encircled-area',
  templateUrl: './encircled-area.component.html',
  styleUrls: ['./encircled-area.component.css']
})
export class EncircledAreaComponent implements OnInit,AfterViewInit {
  @ViewChild(OneOperationComponent) operation: OneOperationComponent;

  id :Array<string> =[];
  table;
  constructor(
    private router: Router,
    private route: ActivatedRoute,
    private tableUtil: TableUtil,
    private dialogService: OneDialogService,
    private encircledAreaService: EncircledAreaService,
    private responseUtil:ResponseUtil,
    private pageSubject: PageSubject
  ) { }

  //按钮点击触及事件
  proxy = {
    //添加，修改，删除
    "add": () => this.add(),
    "update": () => this.update(),
    "areaDelete": () => this.areaDelete()
  }


  ngOnInit() {
    this.pushPage();
    this.loadArea();
  }


  private pushPage() {
    let path = new Path("电子合围区", null);
    this.pageSubject.pushPage("电子合围区管理", "查询", path);
  }

  search(){
    this.table.bootstrapTable('refresh');
  }

  ngAfterViewInit() {
    //操作函数代理
    this.operation.proxy = this.proxy;
  }
  private loadArea() {
    this.table = this.tableUtil.load("one_table_area",{
      columns:[
        { checkbox: true ,align:'',width: 40},
        { field: "id", title:'ID',align:'left',visible:false,switchable:false	},
        { field: "areaCode", title:'合围区编码',align:'left',width: 40},
        { field: "areaName", title:'合围区名称',align:'left',width: 120 },
        { field: "siteCode", title:'站点编码',align:'left' ,width: 40},
      ],
      idField:"id",
      sidePagination:'server',
      pagination:true,
      pageNumber:1,
      pageSize:15,
      pageList:[10, 25, 50, 100],
      paginationPreText:"上一页",
      paginationNextText:"下一页",
      paginationLoop:false,//是否开启循环分页
      singleSelect:false,//多选
      clickToSelect:true,//点击选中
      toolbar:".one-operation",
      url:DamAPI.areaPageListV2,
      method:'post',
      //传递附加参数
      queryParams:(params) => {
        params.id = $("#id").val();
        params.areaName = $("#areaName").val();
        return params;
      },
      //返回数据处理器
      responseHandler:(result)=>{
        return this.responseUtil.bsPageHandler(result,(result)=>{
          console.log(JSON.stringify(result));
        });
      },
      height:700,//table高度
      // sortable:true,
      // order:'asc',
      // sortName:'UPDATEDATE',
      showColumns:true,//显示自定义显示列按钮
      showRefresh:true,//显示刷新按钮
      minimumCountColumns:3,//最少保留显示的列数量
      showToggle:true,//显示视图切换按钮
      showPaginationSwitch:true,//显示是否分页切换按钮
      //css
      classes:"table table-hover",
      striped:true,//隔行变色
      mobileResponsive:true//响应式
    });
  }
  private areaDelete(){
    let rows = this.table.bootstrapTable('getSelections');
    this.id=this.tableUtil.getCloumns(rows,"id");
    if ( undefined == this.id || this.id.length == 0 ) {
      this.dialogService.modalInfo("请选择一条数据");
    } else {
      this.dialogService.modalInfoWithCancelBtn("确定删除？", this.requestDelete);
    }
  }

  private requestDelete = () =>{
      this.encircledAreaService.deletes(this.id).subscribe(
        result => this.responseUtil.success(result, this.processAreaDeleteResult),
        message => this.responseUtil.error(message)
      );

  }


  /**
   * 单一删除结果处理
   * @param result
   */
  processAreaDeleteResult = (result: MsgResult) => {
    if (result && result.code == MsgConstants.MSG_CODE_OK) {
      this.search();
    }
    if (result && result.code != MsgConstants.MSG_CODE_OK) {
      this.dialogService.info(result.message);
    }
  }

  private update(){
    let rows = this.table.bootstrapTable('getSelections');
    this.id = this.tableUtil.getCloumns(rows,"id");
    if (this.id.length != 1) {
      this.dialogService.modalInfo("请选择一条数据");
    } else {
      this.router.navigate(['update',{id:this.id[0]}],{relativeTo :this.route});
    }
  }

  private add(){
    this.router.navigate(['add'],{relativeTo :this.route});
  }

}
